package com.chinalife.project.entity;

import com.chinalife.project.enums.ResponseCodeEnums;
import lombok.Data;

/**
 * @author 18101119
 * @description 返回值的统一封装
 * @date 2024/3/25 13:55
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
@Data
public class ResponseEntity<T> {

    /**
     * 状态码
     */
    protected int code;

    /**
     * 响应信息
     */
    protected String message;

    /**
     * traceId
     */
    protected String traceId;

    /**
     * 返回数据
     */
    private T data;

    public static <T> ResponseEntity<T> success() {
        return new ResponseEntity<>();
    }

    public static <T> ResponseEntity<T> success(T data) {
        return new ResponseEntity<>(data);
    }

    public static <T> ResponseEntity<T> fail(String message) {
        return new ResponseEntity<>(ResponseCodeEnums.FAIL.getCode(), message);
    }

    public static <T> ResponseEntity<T> fail(String message, String traceId) {
        return new ResponseEntity<>(ResponseCodeEnums.FAIL.getCode(), message, null, traceId);
    }

    public static <T> ResponseEntity<T> fail(int code, String message, String traceId) {
        return new ResponseEntity<>(code, message, null, traceId);
    }

    public ResponseEntity() {
        this.code = ResponseCodeEnums.SUCCESS.getCode();
        this.message = ResponseCodeEnums.SUCCESS.getMessage();
    }

    public ResponseEntity(ResponseCodeEnums statusEnums) {
        this.code = statusEnums.getCode();
        this.message = statusEnums.getMessage();
    }

    /**
     * 若没有数据返回，可以人为指定状态码和提示信息
     */
    public ResponseEntity(int code, String msg) {
        this.code = code;
        this.message = msg;
    }

    /**
     * 有数据返回时，状态码为200，默认提示信息为“操作成功！”
     */
    public ResponseEntity(T data) {
        this.data = data;
        this.code = ResponseCodeEnums.SUCCESS.getCode();
        this.message = ResponseCodeEnums.SUCCESS.getMessage();
    }

    /**
     * 有数据返回，状态码为 200，人为指定提示信息
     */
    public ResponseEntity(T data, String msg) {
        this.data = data;
        this.code = ResponseCodeEnums.SUCCESS.getCode();
        this.message = msg;
    }

    /**
     * 有数据返回
     */
    public ResponseEntity(int code, String msg, T data) {
        this.data = data;
        this.code = code;
        this.message = msg;
    }

    /**
     * 有数据返回
     */
    public ResponseEntity(int code, String msg, T data, String traceId) {
        this.data = data;
        this.code = code;
        this.message = msg;
        this.traceId = traceId;
    }
}